package com.weasel.weixin.mp;

import com.weasel.helper.ClassPathFileReader;
import com.weasel.weixin.commons.exception.UnSupportFileException;
import com.weasel.weixin.commons.media.ForeverMediaCountResponse;
import com.weasel.weixin.commons.media.ForeverMediaListResponse;
import com.weasel.weixin.commons.media.ForeverMediaResponse;
import com.weasel.weixin.commons.media.bean.ForeverVideo;
import com.weasel.weixin.commons.media.bean.Image;
import com.weasel.weixin.commons.media.bean.Media;
import com.weasel.weixin.commons.media.bean.Voice;
import com.weasel.weixin.mp.builder.MpConfigBuilder;
import com.weasel.weixin.mp.config.MpConfig;
import com.weasel.weixin.mp.request.MpForeverMediaRequest;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.ExecutionException;

/**永久素材测试
 * 运行main方法，通过注释和打开注释来测试各方法
 * Created by Dylan on 2015/9/29.
 */
public class MpForeverMediaRequestDemo {

    public static void main(String [] args) throws IOException, ExecutionException {

        MpConfig config = MpConfigBuilder.fromXML("classpath:weixin-config.xml");

        MpForeverMediaRequest request = new MpForeverMediaRequest(config);

        //image(request);

       // video(request);

       // voice(request);

       // count(request);

          list(request);

    }

    private static void image(MpForeverMediaRequest request) throws IOException, ExecutionException {
        InputStream fileInputStream = ClassPathFileReader.readToInputStrream("classpath:mm.jpeg");
        File file = new File("media.jpeg");
        FileUtils.copyInputStreamToFile(fileInputStream, file);
        Media image = new Image();
        image.setFile(file);
        ForeverMediaResponse response = request.upload(image);
        if(!response.isOK()){
            throw new UnSupportFileException(response.getErrcode() + " : " + response.getErrmsg());
        }
        request.download(response.getMediaId(), ClassPathFileReader.getClassPath());
    }

    private static void video(MpForeverMediaRequest request) throws IOException, ExecutionException {
        InputStream fileInputStream = ClassPathFileReader.readToInputStrream("classpath:mm.mp4");
        File file = new File("media.mp4");
        FileUtils.copyInputStreamToFile(fileInputStream, file);
        ForeverVideo video = new ForeverVideo();
        video.setDescription(new ForeverVideo.Description("这是语音", "好动听的语音"));
        video.setFile(file);
        ForeverMediaResponse response = request.upload(video);

        if(!response.isOK()){
            throw new UnSupportFileException(response.getErrmsg());
        }
        ForeverMediaResponse _response = request.download(response.getMediaId(),ClassPathFileReader.getClassPath());
    }

    private static void voice(MpForeverMediaRequest request) throws IOException, ExecutionException {
        InputStream fileInputStream = ClassPathFileReader.readToInputStrream("classpath:123.mp3");
        File file = new File("media.mp3");
        FileUtils.copyInputStreamToFile(fileInputStream, file);
        Media voice = new Voice();
        voice.setFile(file);
        ForeverMediaResponse response = request.upload(voice);
        if(!response.isOK()){
            throw new UnSupportFileException(response.getErrmsg());
        }
        ForeverMediaResponse _response = request.download(response.getMediaId(), ClassPathFileReader.getClassPath());
    }

    private static void delete(MpForeverMediaRequest request) throws IOException, ExecutionException {
        ForeverMediaResponse response = request.delete("2aLe038sgCjfr5NNeZF-fZJHHbObK4WrGyGkFqmrRtw");
        System.out.print(response);
    }

    private static void count(MpForeverMediaRequest request) throws IOException, ExecutionException {
        ForeverMediaCountResponse response = request.count();
        System.out.print(response);
    }

    private static void list(MpForeverMediaRequest request) throws IOException, ExecutionException {

        ForeverMediaListResponse response = request.list(new Image(), 0, 20);
        System.out.print(response);
    }
}
